package com.ghsc.ghxc.jsb.ui.message.activity

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.lifecycle.Observer
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GsonUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.ActivityChatUpNameBinding

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.message.activity
 * 类名：ChatGroupNameActivity
 * 创建时间：2024/5/9 11:57
 * 创建人 @author： xiaolin
 * 描述 ：Todo... 修改群名称
 */
class ChatGroupNameActivity  : BaseVMActivity<ChatViewModel, ActivityChatUpNameBinding>()  {
    override fun attachLayoutRes(): Int {
        return  R.layout.activity_chat_up_name
    }


    var bundle : Bundle?=null
    var sign:Int=0
    var content:String =""
    var targetId:String=""

    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }
    override fun initView() {
        bundle=intent.extras
        sign=bundle!!.getInt("sign",1)
        content=bundle!!.getString("content","")
        targetId=bundle!!.getString("targetId","")
        binding.run {
            if (sign==1) {
                bar.topTitle.text="修改群名称"
                llName.visibility= View.VISIBLE
                llContent.visibility= View.GONE
                etName.setText(content)
                etName.hint="请输入群名称"
            }
            else if (sign==2) {
                bar.topTitle.text= "修改群公告"
                llName.visibility= View.GONE
                llContent.visibility= View.VISIBLE
                etContent.setText(content)
            }
            else if (sign==3) {
                bar.topTitle.text="修改备注"
                llName.visibility= View.VISIBLE
                llContent.visibility= View.GONE
                etName.setText(content)
                etName.hint="请输入好友备注"
            }
            bar.topLeftView.setOnClickListener {
                hideKeyboard(etName)
                finish()
            }
        }

    }


    override fun initData() {
        super.initData()
        binding.tvBtnNext.setSingleClickListener {
            if (sign==1){
                if (TextUtils.isEmpty(binding.etName.text)){
                    showToast("请输入群名称")
                    return@setSingleClickListener
                }
                //修改群名称
                val map: HashMap<String, Any> = HashMap()
                map["name"] = binding.etName.text.toString().trim()
                map["gid"]=targetId
                GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onUpGroupInfoMethod(it1) }
            }else if (sign==2){
                if (TextUtils.isEmpty(binding.etContent.text)){
                    showToast("请输入群公告")
                    return@setSingleClickListener
                }
                //修改群名称
                val map: HashMap<String, Any> = HashMap()
                map["gonggao"] = binding.etContent.text.toString().trim()
                map["gid"]=targetId
                GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onUpGroupInfoMethod(it1) }
            }else if (sign==3){
                if (TextUtils.isEmpty(binding.etName.text)){
                    showToast("请输入好友备注")
                    return@setSingleClickListener
                }
                // 备注
                val map: HashMap<String, Any> = HashMap()
                map["memo"] = binding.etName.text.toString().trim()
                map["fuid"]=targetId
                GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onUpFriendMemoMethod(it1) }
            }
        }
    }

    override fun attachVMClass(): Class<ChatViewModel>? {
        return ChatViewModel::class.java
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@ChatGroupNameActivity) {
                showToast(it)
            }
            againLoginMsg.observe(this@ChatGroupNameActivity, Observer {
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            })
            onUpFriendMemoData.observe(this@ChatGroupNameActivity){
                hideKeyboard(binding.etName)
                showToast("修改成功！")
                finish()
            }
            onUpGroupInfoData.observe(this@ChatGroupNameActivity){
                hideKeyboard(binding.etName)
                showToast("修改成功！")
                finish()
            }
        }
    }

}

